/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { md5 } from "./md5.js";

let content = 'ABCDEFGHIJKabcdefj123456+/AA==!@一段中文';
let content1k = ""
let content5k = ""

let dic = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
for (let i = 0; i < 1024; i++) {
    content1k += dic[i / 10]
}
let num = 5120
for (let i = 0; i < num; i++) {
    content5k += dic[i / 10]
}

function md5_Test_01() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 1000; i++) {
        result = md5(content)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5(string) error'
    }
    print(`md5_md5(string): ${endTime - startTime} ms`)
}

function md5_Test_02() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 100; i++) {
        result = md5(content1k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5(string1k) error'
    }
    print(`md5_md5(string1k): ${endTime - startTime} ms`)
}

function md5_Test_03() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 10; i++) {
        result = md5(content5k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5(string5k) error'
    }
    print(`md5_md5(string5k): ${endTime - startTime} ms`)
}

function md5_Test_04() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 1000; i++) {
        result = md5.base64(content)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.base64(content) error'
    }
    print(`md5_base64(string): ${endTime - startTime} ms`)
}

function md5_Test_05() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 100; i++) {
        result = md5.base64(content1k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.base64(content1k) error'
    }
    print(`md5_base64(string1k): ${endTime - startTime} ms`)
}

function md5_Test_06() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 10; i++) {
        result = md5.base64(content5k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.base64(content5k) error'
    }
    print(`md5_base64(string5k): ${endTime - startTime} ms`)
}

function md5_Test_07() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 1000; i++) {
        result = md5.hex(content)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.hex(content) error'
    }
    print(`md5_hex(string): ${endTime - startTime} ms`)
}

function md5_Test_08() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 100; i++) {
        result = md5.hex(content1k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.hex(content1k) error'
    }
    print(`md5_hex(string1k): ${endTime - startTime} ms`)
}

function md5_Test_09() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 10; i++) {
        result = md5.hex(content5k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.hex(content5k) error'
    }
    print(`md5_hex(string5k): ${endTime - startTime} ms`)
}

function md5_Test_10() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 1000; i++) {
        result = md5.array(content)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.array(content) error'
    }
    print(`md5_array(string): ${endTime - startTime} ms`)
}

function md5_Test_11() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 100; i++) {
        result = md5.array(content1k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.array(content1k) error'
    }
    print(`md5_array(string1k): ${endTime - startTime} ms`)
}

function md5_Test_12() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 10; i++) {
        result = md5.array(content5k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.array(content) error'
    }
    print(`md5_array(string5k): ${endTime - startTime} ms`)
}

function md5_Test_13() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 1000; i++) {
        result = md5.digest(content)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.digest(content) error'
    }
    print(`md5_digest(string): ${endTime - startTime} ms`)
}

function md5_Test_14() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 100; i++) {
        result = md5.digest(content1k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.digest(content1k) error'
    }
    print(`md5_digest(string1k): ${endTime - startTime} ms`)
}

function md5_Test_15() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 10; i++) {
        result = md5.digest(content5k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.digest(content) error'
    }
    print(`md5_digest(string5k): ${endTime - startTime} ms`)
}

function md5_Test_16() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 1000; i++) {
        result = md5.arrayBuffer(content)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.arrayBuffer(content) error'
    }
    print(`md5_arrayBuffer(string): ${endTime - startTime} ms`)
}

function md5_Test_17() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 100; i++) {
        result = md5.arrayBuffer(content1k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.arrayBuffer(content1k) error'
    }
    print(`md5_arrayBuffer(string1k): ${endTime - startTime} ms`)
}

function md5_Test_18() {
    let result = null
    let startTime = Date.now()
    for (let i = 0; i < 10; i++) {
        result = md5.arrayBuffer(content5k)
    }
    let endTime = Date.now()
    if (result == null) {
        throw 'md5.arrayBuffer(content) error'
    }
    print(`md5_arrayBuffer(string5k): ${endTime - startTime} ms`)
}

let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
    md5_Test_01()
    md5_Test_02()
    md5_Test_03()
    md5_Test_04()
    md5_Test_05()
    md5_Test_06()
    md5_Test_07()
    md5_Test_08()
    md5_Test_09()
    md5_Test_10()
    md5_Test_11()
    md5_Test_12()
    md5_Test_13()
    md5_Test_14()
    md5_Test_15()
    md5_Test_16()
    md5_Test_17()
    md5_Test_18()
}

ArkTools.waitAllJitCompileFinish();

md5_Test_01()
md5_Test_02()
md5_Test_03()
md5_Test_04()
md5_Test_05()
md5_Test_06()
md5_Test_07()
md5_Test_08()
md5_Test_09()
md5_Test_10()
md5_Test_11()
md5_Test_12()
md5_Test_13()
md5_Test_14()
md5_Test_15()
md5_Test_16()
md5_Test_17()
md5_Test_18()